<template>
	<view>
		<view class="content">
			<view class="qingchu" @tap="clearCache">
				<view class="text">清除缓存</view>
				<view class="huncun text">{{fileSizeString}}</view>
				<image src="../../static/image/qianjin.png" mode="" class="qingchu1"></image>
			</view>
			<view class="qingchu" >
				<view class="text" style="height:50rpx;line-height: 50rpx;">勿扰模式</view>
				<switch :checked="wurao" style="flex:1;text-align: right;margin-right: 30rpx;" @change="switch1Change"/>
			</view>
			<view class="qingchu" @tap="clickGengxin">
				<view class="text">版本</view>
				<view class="huncun text">{{version.versionCode}}</view>
				<image src="../../static/image/qianjin.png" mode="" class="qingchu1"></image>
			</view>
			<view class="qingchu" @tap="toyonghuXieyi()" style="border: unset;">
				<view class="text">隐私政策</view>
				<view class="huncun text">&nbsp;</view>
				<image src="../../static/image/qianjin.png" mode="" class="qingchu1"></image>
			</view>
		</view>
		<view class="content-padding">
			<view class="denglv" @tap="submit()">
				退出登录
			</view>
		</view>
		<selfPopupMessage ref="selfPopupMessage"></selfPopupMessage>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				fileSizeString:"0kb",
				wurao:false,
				version:uni.getStorageSync("version")
			}
		},
		onLoad() {
			this.formatSize();
			const user = uni.getStorageSync("userInfo");
			this.wurao = user.wurao==1;
		},
		methods: {
			clickGengxin:function(){
				console.log(this.version.newUrl);
				if(this.version.newUrl==undefined){
					this.$refs.uToast.show({
						title: '已是最新版本',
						duration: 2000
					})
					return;
				}
				plus.runtime.openURL(this.version.newUrl);  
			},
			formatSize() {  
				let that = this;  
				plus.cache.calculate(function(size) {  
					let sizeCache = parseInt(size);  
					if (sizeCache == 0) {  
						that.fileSizeString = "0B";  
					} else if (sizeCache < 1024) {  
						that.fileSizeString = sizeCache + "B";  
					} else if (sizeCache < 1048576) {  
						that.fileSizeString = (sizeCache / 1024).toFixed(2) + "KB";  
					} else if (sizeCache < 1073741824) {  
						that.fileSizeString = (sizeCache / 1048576).toFixed(2) + "MB";  
					} else {  
						that.fileSizeString = (sizeCache / 1073741824).toFixed(2) + "GB";  
					}  
				});  
			},
			clearCache() {  
				// uni.clearStorageSync();
				let that = this;  
				let os = plus.os.name;  
				if (os == 'Android') {  
					let main = plus.android.runtimeMainActivity();  
					let sdRoot = main.getCacheDir();  
					let files = plus.android.invoke(sdRoot, "listFiles");  
					let len = files.length;  
					for (let i = 0; i < len; i++) {  
						let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径  
						plus.io.resolveLocalFileSystemURL(filePath, function(entry) {  
							if (entry.isDirectory) {  
								entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录  
									that.$refs.selfPopupMessage.open("缓存清理完成");
									that.formatSize(); // 重新计算缓存  
								}, function(e) {  
									console.log(e.message)  
								});  
							} else {  
								entry.remove();  
							}  
						}, function(e) {  
							console.log('文件路径读取失败')  
						});  
					}  
				} else { // ios暂时未找到清理缓存的方法，以下是官方提供的方法，但是无效，会报错  
					plus.cache.clear(function() {  
						uni.showToast({  
							title: '缓存清理完成',  
							duration: 2000  
						});  
						that.formatSize();  
					});  
				}  
			},
			toyonghuXieyi:function(){
				this.$util.toUrl("../login/xieyi");
			},
			switch1Change: function (e) {
				const userId = uni.getStorageSync("token");
				var wurao = 0;
				var str = "";
				if(e.target.value){
					wurao = 1;
					str = "开启";
				}else{
					wurao = 0;
					str = "关闭"
				}
				var that = this ;
				this.$util.nuiAjax(this.$jiekou.updateUser,{userId:userId,wurao:wurao},function(res){
					if(res.result=="true"){
						that.$refs.selfPopupMessage.open(str+"勿扰模式成功");
						uni.$emit("wurao",{wurao:wurao});
						const user = uni.getStorageSync("userInfo");
						user.wurao = wurao;
						uni.setStorageSync("userInfo",user);
					}
				})	
			},
			submit(){
				uni.removeStorageSync("token");
				uni.removeStorageSync("userInfo");
				// plus.runtime.restart();
				var that = this;
				
				uni.reLaunch({
					url:"../login/login"
				})
				
				let promise = this.tim.logout();
				promise.then(function(imResponse) {
				  console.log(imResponse.data); // 登出成功
				}).catch(function(imError) {
				  console.warn('logout error:', imError);
				});
				this.$store.commit('reset');
				// uni.switchTab({
				//     url: '/pages/index/index',
				// 	success:function(){
				// 		that.$util.toUrl("../login/login");
				// 	}
				// });
			}
		}
	}
</script>

<style lang="scss">
	.content-padding{
		padding: 0 54upx 0 54upx;
	}
	.denglv{
		height:96rpx;
		background: $uni-color-primary;
		border-radius:48rpx;
		margin-top: 80rpx;
		line-height: 96rpx;
		text-align: center;
		font-size: 32rpx;
		color: $uni-text-color-inverse;
		letter-spacing: 1rpx;
	}
	page{
		background-color: $page-bg-color;
	}
	.content{
		background-color: #FFFFFF;
		margin-top: 20rpx;
		padding-left: 30rpx;
		box-shadow:0px 8px 16px 0px rgba(246,247,248,1);
	}
	.qingchu{
		display: flex;
		flex-flow: row;
		// justify-content: space-between;
		padding-top: 30rpx;
		padding-bottom: 28rpx;
		border-bottom: 1px solid #EFEFEF;
	}
	.huncun{
		flex:1;
		text-align: right;
		margin-right: 20rpx;
	}
	.qingchu1{
		margin-right: 30rpx;
		width: 10rpx;
		height: 20rpx;
		margin-top: 12rpx;
	}
	.text{
		height:40rpx;
		font-size:28rpx;
		font-weight:400;
		line-height:40rpx;
	}
</style>
